//
//  CCFindDetailViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFindDetailViewController.h"
#import "BaseHeader.h"
#import "FindDetaillTableViewCell.h"

#import "CarDetailModel.h"
@interface CCFindDetailViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (nonatomic , strong) UIImageView * headImage ;
@property (nonatomic , strong) UILabel * priceLabel ;

@property (nonatomic , strong) UILabel * sgLabel ;

@property (nonatomic , strong) UILabel * cityLabel ;
@property (nonatomic , strong) UILabel * kindLabel ;
@property (nonatomic , strong) UITableView * myTableView ;

@property (nonatomic , strong) NSMutableArray * infoArray ;
@property (nonatomic , strong)NSMutableArray * saArray;
@property (nonatomic , strong)NSMutableDictionary * seDic;
@end

@implementation CCFindDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"找车详情";
    _infoArray = [[NSMutableArray alloc]init];
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 350 * suitableScreen_Width)];
        
    _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 300 * suitableScreen_Width)];
    [headView addSubview:_headImage];
    _kindLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 260 * suitableScreen_Width, 300 * suitableScreen_Width, 40 * suitableScreen_Width)];
    [_kindLabel setTextColor:[UIColor whiteColor]];
    [_headImage addSubview:_kindLabel];
    
    _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 * suitableScreen_Width, 300 * suitableScreen_Width, 300 * suitableScreen_Width, 25 * suitableScreen_Width)];
    [headView addSubview:_priceLabel];
    _sgLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 * suitableScreen_Width, 325 * suitableScreen_Width, mainScreen_Width / 2, 25 * suitableScreen_Width)];
    [headView addSubview:_sgLabel];
    
    _cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 2, 325 * suitableScreen_Width, mainScreen_Width / 2, 25 * suitableScreen_Width)];
    [headView addSubview:_cityLabel];
    
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height - 30 * suitableScreen_Width ) style:UITableViewStyleGrouped];
    _myTableView.delegate = self ;
    _myTableView.dataSource = self ;
    [self.view addSubview:_myTableView];
    _myTableView.tableHeaderView = headView ;
    [self getData];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [_myTableView setBackgroundColor:[UIColor whiteColor]];
}

-(void)getData{
    [AFNblock getdataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/xsp/s/auto/info/v4.7/serials.xsp?fmt=json&serialId=%@&hasDealers=1&regionId=248",self.model.cid] block:^(id block) {
        NSLog(@"%@",self.model.cid);
        [_headImage sd_setImageWithURL:[NSURL URLWithString:[block objectForKey:@"photo_400x300"]]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        
        NSString * nameStr = [block objectForKey:@"manufacturer"];
        NSString * kindStr = [block objectForKey:@"kind"];
        
        [_kindLabel setText:[NSString stringWithFormat:@"%@-%@",nameStr,kindStr]];
        NSString * pStr = [block objectForKey:@"priceRange"];
        NSMutableAttributedString * mutableString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"已报名%@人",pStr]];
        
      
        
        NSInteger len = pStr.length - 1 ;
        [mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, len)];
        self.priceLabel.attributedText = mutableString ;        [_priceLabel setFont:[UIFont systemFontOfSize:14 * suitableScreen_Width]];
        NSNumber * sg = [block objectForKey:@"sgAverageScore"];
        [_sgLabel setText:[NSString stringWithFormat:@"综合评分%@分>",sg]];
        [_sgLabel setFont:[UIFont systemFontOfSize:14 * suitableScreen_Width]];
        NSString * steCity = [block objectForKey:@"city"];
        NSNumber * countNum = [block objectForKey:@"dealerCount"];
       
        NSMutableAttributedString * mutableStringC = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@经销商%@家",steCity,countNum]];
        
        NSString * coo = [NSString stringWithFormat:@"%@",countNum];
        
        NSInteger lenc = coo.length;
        [mutableStringC addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, lenc )];
        self.cityLabel.attributedText = mutableStringC ;
        
       
        [_cityLabel setFont:[UIFont systemFontOfSize:14 * suitableScreen_Width]];
        _saArray = [block objectForKey:@"sales"];
        if (_saArray.count != 0) {
            
      _seDic = [_saArray objectAtIndex:0];
        
        NSMutableArray * dataArray = [_seDic objectForKey:@"data"];
        for (NSMutableDictionary * dataDic in dataArray) {
          CarDetailModel * model = [CarDetailModel modelWithDictionary:dataDic];
            [_infoArray addObject:model];
        }
        }else{
            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0 , 400 * suitableScreen_Width * suitableScreen_Width, mainScreen_Width, 50 * suitableScreen_Width)];
            [_myTableView addSubview:label];
            [label setText:@"当前没有信息 !"];
            [label setTextColor:[UIColor redColor]];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setFont:[UIFont systemFontOfSize:24]];
        }
        
        [_myTableView reloadData];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _infoArray.count ; 
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 105 * suitableScreen_Width ;
}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   NSString * cellID = @"cell";
    
    FindDetaillTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        
        cell = [[FindDetaillTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    CarDetailModel * model = [_infoArray objectAtIndex:indexPath.row];
    [cell getInfoModel:model];
    return cell ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
